﻿namespace Microsoft.SharePoint.Client
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;

    [ScriptType("SP.Site", ServerTypeId="{e1bb82e8-0d1e-4e52-b90c-684802ab4ef6}")]
    public class Site : ClientObject
    {
        [EditorBrowsable(EditorBrowsableState.Never)]
        public Site(ClientRuntimeContext Context, ObjectPath ObjectPath) : base(Context, ObjectPath)
        {
        }

        [Remote]
        public List GetCatalog(int typeCatalog)
        {
            Dictionary<int, List> dictionary;
            object obj2;
            if (base.ObjectData.MethodReturnObjects.TryGetValue("GetCatalog", out obj2))
            {
                dictionary = (Dictionary<int, List>) obj2;
            }
            else
            {
                dictionary = new Dictionary<int, List>();
                base.ObjectData.MethodReturnObjects["GetCatalog"] = dictionary;
            }
            List list = null;
            if (!dictionary.TryGetValue(typeCatalog, out list))
            {
                list = new List(base.Context, new ObjectPathMethod(base.Context, base.Path, "GetCatalog", new object[] { typeCatalog }));
                dictionary[typeCatalog] = list;
            }
            return list;
        }

        [Remote]
        public ChangeCollection GetChanges(ChangeQuery query_)
        {
            return new ChangeCollection(base.Context, new ObjectPathMethod(base.Context, base.Path, "GetChanges", new object[] { query_ }));
        }

        [Remote]
        public ListTemplateCollection GetCustomListTemplates(Web web)
        {
            return new ListTemplateCollection(base.Context, new ObjectPathMethod(base.Context, base.Path, "GetCustomListTemplates", new object[] { web }));
        }

        protected override bool InitOnePropertyFromJson(string peekedName, JsonReader reader)
        {
            bool flag = base.InitOnePropertyFromJson(peekedName, reader);
            if (!flag)
            {
                switch (peekedName)
                {
                    case "Id":
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["Id"] = reader.ReadGuid();
                        return flag;

                    case "ServerRelativeUrl":
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["ServerRelativeUrl"] = reader.ReadString();
                        return flag;

                    case "Url":
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["Url"] = reader.ReadString();
                        return flag;

                    case "Features":
                        flag = true;
                        reader.ReadName();
                        this.Features.FromJson(reader);
                        return flag;

                    case "UserCustomActions":
                        flag = true;
                        reader.ReadName();
                        this.UserCustomActions.FromJson(reader);
                        return flag;

                    case "RootWeb":
                        flag = true;
                        reader.ReadName();
                        this.RootWeb.FromJson(reader);
                        return flag;

                    case "Usage":
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["Usage"] = reader.Read<UsageInfo>();
                        return flag;

                    case "UIVersionConfigurationEnabled":
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["UIVersionConfigurationEnabled"] = reader.ReadBoolean();
                        return flag;

                    case "RecycleBin":
                        flag = true;
                        reader.ReadName();
                        this.RecycleBin.FromJson(reader);
                        return flag;

                    case "MaxItemsPerThrottledOperation":
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["MaxItemsPerThrottledOperation"] = reader.ReadUInt32();
                        return flag;

                    case "AllowDesigner":
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["AllowDesigner"] = reader.ReadBoolean();
                        return flag;

                    case "AllowRevertFromTemplate":
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["AllowRevertFromTemplate"] = reader.ReadBoolean();
                        return flag;

                    case "AllowMasterPageEditing":
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["AllowMasterPageEditing"] = reader.ReadBoolean();
                        return flag;

                    case "ShowUrlStructure":
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["ShowUrlStructure"] = reader.ReadBoolean();
                        return flag;
                }
            }
            return flag;
        }

        [Remote]
        public Web OpenWeb(string strUrl)
        {
            Dictionary<string, Web> dictionary;
            object obj2;
            if (base.Context.ValidateOnClient && (strUrl == null))
            {
                throw ClientUtility.CreateArgumentNullException("strUrl");
            }
            if (base.ObjectData.MethodReturnObjects.TryGetValue("OpenWeb", out obj2))
            {
                dictionary = (Dictionary<string, Web>) obj2;
            }
            else
            {
                dictionary = new Dictionary<string, Web>(StringComparer.OrdinalIgnoreCase);
                base.ObjectData.MethodReturnObjects["OpenWeb"] = dictionary;
            }
            Web web = null;
            if (!dictionary.TryGetValue(strUrl, out web))
            {
                web = new Web(base.Context, new ObjectPathMethod(base.Context, base.Path, "OpenWeb", new object[] { strUrl }));
                dictionary[strUrl] = web;
                ObjectIdentityQuery query = new ObjectIdentityQuery(web.Path);
                base.Context.AddQueryIdAndResultObject(query.Id, web);
                base.Context.AddQuery(query);
            }
            return web;
        }

        [Remote]
        public Web OpenWebById(Guid gWebId)
        {
            return new Web(base.Context, new ObjectPathMethod(base.Context, base.Path, "OpenWebById", new object[] { gWebId }));
        }

        [Remote]
        public bool AllowDesigner
        {
            get
            {
                base.CheckUninitializedProperty("AllowDesigner");
                return (bool) base.ObjectData.Properties["AllowDesigner"];
            }
            set
            {
                base.ObjectData.Properties["AllowDesigner"] = value;
                if (base.Context != null)
                {
                    base.Context.AddQuery(new ClientActionSetProperty(this, "AllowDesigner", value));
                }
            }
        }

        [Remote]
        public bool AllowMasterPageEditing
        {
            get
            {
                base.CheckUninitializedProperty("AllowMasterPageEditing");
                return (bool) base.ObjectData.Properties["AllowMasterPageEditing"];
            }
            set
            {
                base.ObjectData.Properties["AllowMasterPageEditing"] = value;
                if (base.Context != null)
                {
                    base.Context.AddQuery(new ClientActionSetProperty(this, "AllowMasterPageEditing", value));
                }
            }
        }

        [Remote]
        public bool AllowRevertFromTemplate
        {
            get
            {
                base.CheckUninitializedProperty("AllowRevertFromTemplate");
                return (bool) base.ObjectData.Properties["AllowRevertFromTemplate"];
            }
            set
            {
                base.ObjectData.Properties["AllowRevertFromTemplate"] = value;
                if (base.Context != null)
                {
                    base.Context.AddQuery(new ClientActionSetProperty(this, "AllowRevertFromTemplate", value));
                }
            }
        }

        [Remote]
        public FeatureCollection Features
        {
            get
            {
                object obj2;
                if (base.ObjectData.ClientObjectProperties.TryGetValue("Features", out obj2))
                {
                    return (FeatureCollection) obj2;
                }
                FeatureCollection features = new FeatureCollection(base.Context, new ObjectPathProperty(base.Context, base.Path, "Features"));
                base.ObjectData.ClientObjectProperties["Features"] = features;
                return features;
            }
        }

        [Remote]
        public Guid Id
        {
            get
            {
                base.CheckUninitializedProperty("Id");
                return (Guid) base.ObjectData.Properties["Id"];
            }
        }

        [Remote]
        public uint MaxItemsPerThrottledOperation
        {
            get
            {
                base.CheckUninitializedProperty("MaxItemsPerThrottledOperation");
                return (uint) base.ObjectData.Properties["MaxItemsPerThrottledOperation"];
            }
        }

        [Remote]
        public RecycleBinItemCollection RecycleBin
        {
            get
            {
                object obj2;
                if (base.ObjectData.ClientObjectProperties.TryGetValue("RecycleBin", out obj2))
                {
                    return (RecycleBinItemCollection) obj2;
                }
                RecycleBinItemCollection items = new RecycleBinItemCollection(base.Context, new ObjectPathProperty(base.Context, base.Path, "RecycleBin"));
                base.ObjectData.ClientObjectProperties["RecycleBin"] = items;
                return items;
            }
        }

        [Remote]
        public Web RootWeb
        {
            get
            {
                object obj2;
                if (base.ObjectData.ClientObjectProperties.TryGetValue("RootWeb", out obj2))
                {
                    return (Web) obj2;
                }
                Web web = new Web(base.Context, new ObjectPathProperty(base.Context, base.Path, "RootWeb"));
                base.ObjectData.ClientObjectProperties["RootWeb"] = web;
                return web;
            }
        }

        [Remote]
        public string ServerRelativeUrl
        {
            get
            {
                base.CheckUninitializedProperty("ServerRelativeUrl");
                return (string) base.ObjectData.Properties["ServerRelativeUrl"];
            }
        }

        [Remote]
        public bool ShowUrlStructure
        {
            get
            {
                base.CheckUninitializedProperty("ShowUrlStructure");
                return (bool) base.ObjectData.Properties["ShowUrlStructure"];
            }
            set
            {
                base.ObjectData.Properties["ShowUrlStructure"] = value;
                if (base.Context != null)
                {
                    base.Context.AddQuery(new ClientActionSetProperty(this, "ShowUrlStructure", value));
                }
            }
        }

        [Remote]
        public bool UIVersionConfigurationEnabled
        {
            get
            {
                base.CheckUninitializedProperty("UIVersionConfigurationEnabled");
                return (bool) base.ObjectData.Properties["UIVersionConfigurationEnabled"];
            }
            set
            {
                base.ObjectData.Properties["UIVersionConfigurationEnabled"] = value;
                if (base.Context != null)
                {
                    base.Context.AddQuery(new ClientActionSetProperty(this, "UIVersionConfigurationEnabled", value));
                }
            }
        }

        [Remote]
        public string Url
        {
            get
            {
                base.CheckUninitializedProperty("Url");
                return (string) base.ObjectData.Properties["Url"];
            }
        }

        [Remote]
        public UsageInfo Usage
        {
            get
            {
                base.CheckUninitializedProperty("Usage");
                return (UsageInfo) base.ObjectData.Properties["Usage"];
            }
        }

        [Remote]
        public UserCustomActionCollection UserCustomActions
        {
            get
            {
                object obj2;
                if (base.ObjectData.ClientObjectProperties.TryGetValue("UserCustomActions", out obj2))
                {
                    return (UserCustomActionCollection) obj2;
                }
                UserCustomActionCollection actions = new UserCustomActionCollection(base.Context, new ObjectPathProperty(base.Context, base.Path, "UserCustomActions"));
                base.ObjectData.ClientObjectProperties["UserCustomActions"] = actions;
                return actions;
            }
        }
    }
}

